# Copyright (c) 2021 Contributors to the Eclipse Foundation
# 
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
# 
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
# 
# SPDX-License-Identifier: EPL-2.0
#
import os
import re
import pathlib
import datetime
import xml.etree.ElementTree as ET
import html

import nut_utils
    
def msgTextNodeToPRBString(xmlnode):
    '''The node can contain innertext and tags like <q />'''
    strwithtags = ET.tostring(xmlnode).decode()
    
    #Replace <q />'s with double-quotes
    str1      = re.sub("<\s*q[\s/]*>",'\\"',strwithtags)
    #Replace line breaks with spaces
    str2      = re.sub("\n",' ',str1)
    #Replace <br /> tags
    str3      = re.sub("<\s*br[\s/]*>",'\\\\n',str2)
    #Remove starting tag
    str4  = re.sub("<\s*MsgText[^>]*>",'',str3)
    #Remove ending tag
    str5  = re.sub("<\s*/\s*MsgText[^>]*>",'',str4)
    
    #Replace xml angled brackets etc for the real thing
    str6 = html.unescape(str5)
     
    #Unlike ICU we do NOT duplicate single quotes- so not str7 needed
    
    #Surprisingly (to me) - replace any number of spaces with a single space
    str8 = re.sub("\s{2,}", ' ', str6)
    
    return str8.strip()

def outputPRBFile(logger, inputfile, outpath):
    timestr = datetime.datetime.now().strftime("%H:%M:S on %d %B %Y")
    logger.info("Converting "+inputfile+" to PRB output file: "+outpath+" at "+timestr)

    nut_utils.createOutputDir(outpath)
    inputxmlroot = nut_utils.parseFile(inputfile)

    try:
       outfile = open(outpath, "w", encoding="utf8")
       outfile.write("#PRB output file generated by nut_genPRB.py (part of nut_translation.py)\n")
       outfile.write("#Generated from "+inputfile+" at "+timestr+"\n\n")
       
       for child in inputxmlroot:
           if child.tag == "Message":
               msgnode = child
               msgid = None
               msgtext = None

               if msgnode.attrib['ID']:
                   msgid = msgnode.attrib['ID']

               for mchild in msgnode:
                   if mchild.tag == "MsgText":
                       msgtextnode = mchild
                       msgtext = msgTextNodeToPRBString(msgtextnode)
               
               if msgtext and msgid:
                   outfile.write(msgid+'='+msgtext+'\n')
               else:
                   logger.error("Malformed message")
                   if msgid:
                       logger.error("Maformed message ID is "+msgid)
                   else:
                       logger.error("Maformed message ID is MISSING")
                   if msgtext:
                       logger.error("Maformed message text is "+msgtext)
                   else:
                       logger.error("Maformed message text is MISSING")

       outfile.close()

    except Exception as e:
       logger.error("Failed to write PRB txt file: %s - error %s" %
                         (outpath,  e))
       raise e 
    
def outputPRB(logger, replace_filename_vars, infile, langliststr, outpath):

    if langliststr is not None:
        langlist = nut_utils.parseLanguageLists(langliststr)

        for lang in langlist:
            if replace_filename_vars:
                inputfile   = nut_utils.parseFileNameForLangVars(infile, lang)
                outputfile  = nut_utils.parseFileNameForLangVars(outpath, lang)
            else:
                inputfile = infile
                outputfile = outpath

            outputPRBFile(logger, inputfile, outputfile)
    else:
        outputPRBFile(logger, infile, outpath)

